package table

import (
	"fmt"
	"strings"

	"gitee.com/linxing_3/sr-core/domain/service/srvctx"
	"gitee.com/mkwchecking/sr-admin/apps/devtools/acl"
	"github.com/spf13/cobra"
)

var (
	_tableGenArg = &TableGenArg{}
	_tableGenCmd = &cobra.Command{
		Use:     "gen",
		Short:   "generate table",
		PreRunE: dependsCheck,
	}

	_tableGenBackendCmd = &cobra.Command{
		Use:     "back",
		Short:   "generate backend code args: [all/ acl api entity client route test repo service]",
		PreRunE: dependsCheck,
		RunE:    genBackendTableHandler,
	}

	_tableGenFrontendCmd = &cobra.Command{
		Use:     "front",
		Short:   "generate frontend code args: [all/ api view provider]",
		PreRunE: dependsCheck,
		RunE:    genFrontendTableHandler,
	}
)

func init() {

	_tableGenCmd.AddCommand(_tableGenBackendCmd)
	_tableGenArg.Parse(_tableGenBackendCmd)

	_tableGenCmd.AddCommand(_tableGenFrontendCmd)
	_tableGenArg.Parse(_tableGenFrontendCmd)

	tableCmd.AddCommand(_tableGenCmd)
}

func genBackendTableHandler(cmd *cobra.Command, args []string) error {
	req := &acl.ClassGenReq{
		Domain:    _tableGenArg.Domain,
		ClassName: _tableGenArg.Name,
	}
	mapModule := map[string]bool{}
	for _, v := range _tableGenArg.Modules {
		mapModule[strings.ToLower(v)] = true
	}

	for len(mapModule) == 0 {
		// 提示输入指定的模块
		modules := ""
		fmt.Println("请输入需要生成的模块 [all] [acl api entity client route test repo service]:", "使用空格分割:")
		fmt.Scanln(&modules)
		for _, v := range strings.Split(strings.ReplaceAll(modules, "  ", ""), " ") {
			mapModule[v] = true
		}
	}

	if all, ok := mapModule["all"]; ok && all {
		req.BackAcl = true
		req.BackApi = true
		req.BackEntity = true
		req.BackHttpClient = true
		req.BackHttpRouter = true
		req.BackHttpTest = true
		req.BackRepo = true
		req.BackService = true
	} else if _, ok := mapModule["acl"]; ok {
		req.BackAcl = true
	} else if _, ok := mapModule["api"]; ok {
		req.BackApi = true
	} else if _, ok := mapModule["entity"]; ok {
		req.BackEntity = true
	} else if _, ok := mapModule["client"]; ok {
		req.BackHttpClient = true
	} else if _, ok := mapModule["route"]; ok {
		req.BackHttpRouter = true
	} else if _, ok := mapModule["test"]; ok {
		req.BackHttpTest = true
	} else if _, ok := mapModule["repo"]; ok {
		req.BackRepo = true
	} else if _, ok := mapModule["service"]; ok {
		req.BackService = true
	}

	return genCodeFile(cmd, req)
}

func genFrontendTableHandler(cmd *cobra.Command, args []string) error {
	req := &acl.ClassGenReq{
		Domain:    _tableGenArg.Domain,
		ClassName: _tableGenArg.Name,
	}
	mapModule := map[string]bool{}
	for _, v := range _tableGenArg.Modules {
		mapModule[strings.ToLower(v)] = true
	}
	if all, ok := mapModule["all"]; ok && all {
		req.FrontApi = true
		req.FrontView = true
		req.FrontViewProvider = true
	} else if _, ok := mapModule["api"]; ok {
		req.FrontApi = true
	} else if _, ok := mapModule["view"]; ok {
		req.FrontView = true
	} else if _, ok := mapModule["provider"]; ok {
		req.FrontViewProvider = true
	}

	return genCodeFile(cmd, req)
}

func genCodeFile(cmd *cobra.Command, req *acl.ClassGenReq) error {
	ctx := srvctx.NewCmdCtx(cmd)
	err := svc.Gen.ClassGen(ctx, req)
	return err
}
